/*
 * @Description: 线路切割工具类
 * @Author: cgt
 * @Date: 2021-12-22 10:10:50
 * @LastEditors: cgt
 * @LastEditTime: 2021-12-29 15:44:25
 */
class SliceUtil {
  constructor() {

  }
  slice(line, polygon) {
    var intersects = turf.lineIntersect(line, polygon);
    if(intersects.features.length === 2) {
      var [intersectPoint1, intersectPoint2] = intersects.features;
      // 两个交点才符合规则
      // 取交叉点的最近点
      var nearInfo1 = turf.nearestPointOnLine(line, intersectPoint1),
        nearInfo2 = turf.nearestPointOnLine(line, intersectPoint2);
        // 从最近点信息里获取与哪个线段交叉
      var nearSegmentIndex1 = nearInfo1.properties.index,
        nearSegmentIndex2 = nearInfo2.properties.index;
      var segments = turf.lineSegment(line);
      var frontLineCoords = null, betweenLineCoords = null, afterLineCoords = null;
      if(nearSegmentIndex1 === nearSegmentIndex2) {
        // 在同一条线段上
        // 获取交叉的线段
        var intersectSegment = segments.features[nearSegmentIndex1];
        var intersectSegmentCoords = turf.getCoords(intersectSegment);
        // 判断两个交点与起点的距离
        var dis1 = turf.distance(intersectPoint1, turf.point(intersectSegmentCoords[0])),
          dis2 = turf.distance(intersectPoint2, turf.point(intersectSegmentCoords[0]));
        frontLineCoords = segments.features.slice(0, nearSegmentIndex1)
          .map((k) => turf.getCoords(k.geometry))
          .flat();
        afterLineCoords = segments.features.slice(nearSegmentIndex1 + 1)
          .map((k) => turf.getCoords(k.geometry))
          .flat();
        if(dis1 < dis2) {
          // 正序
          frontLineCoords = [...frontLineCoords, ...[intersectSegmentCoords[0], turf.getCoord(intersectPoint1)]];
          afterLineCoords = [...[turf.getCoord(intersectPoint2), intersectSegmentCoords[1]], ...afterLineCoords];
          betweenLineCoords = [turf.getCoord(intersectPoint1), turf.getCoord(intersectPoint2)];
        }else{
          // 反序
          frontLineCoords = [...frontLineCoords, ...[intersectSegmentCoords[0], turf.getCoord(intersectPoint2)]];
          afterLineCoords = [...[turf.getCoord(intersectPoint1), intersectSegmentCoords[1]], ...afterLineCoords];
          betweenLineCoords = [turf.getCoord(intersectPoint2), turf.getCoord(intersectPoint1)];

        }
      }else{
        // 获取交叉的线段
        var intersectSegment1 = segments.features[nearSegmentIndex1];
        var intersectSegmentCoords1 = turf.getCoords(intersectSegment1);
        var intersectSegment2 = segments.features[nearSegmentIndex2];
        var intersectSegmentCoords2 = turf.getCoords(intersectSegment2);
        if(nearSegmentIndex1 < nearSegmentIndex2) {
          frontLineCoords = segments.features.slice(0, nearSegmentIndex1)
            .map((k) => turf.getCoords(k.geometry))
            .flat();
          afterLineCoords = segments.features.slice(nearSegmentIndex2 + 1)
            .map((k) => turf.getCoords(k.geometry))
            .flat();

          frontLineCoords = [...frontLineCoords, ...[intersectSegmentCoords1[0], turf.getCoord(intersectPoint1)]];
          afterLineCoords = [...[turf.getCoord(intersectPoint2), intersectSegmentCoords2[1]], ...afterLineCoords];
          betweenLineCoords = [];
          betweenLineCoords.push(turf.getCoord(intersectPoint1));
          betweenLineCoords.push(intersectSegmentCoords1[1]);
          for(let i = nearSegmentIndex1 + 1;i < nearSegmentIndex2;i++) {
            let _segment = segments.features[i];
            let _coords = turf.getCoords(_segment);
            betweenLineCoords.push(..._coords);
          }
          betweenLineCoords.push(intersectSegmentCoords2[0]);
          betweenLineCoords.push(turf.getCoord(intersectPoint2));
        }else{
          frontLineCoords = segments.features.slice(0, nearSegmentIndex2)
            .map((k) => turf.getCoords(k.geometry))
            .flat();
          afterLineCoords = segments.features.slice(nearSegmentIndex1 + 1)
            .map((k) => turf.getCoords(k.geometry))
            .flat();

          frontLineCoords = [...frontLineCoords, ...[intersectSegmentCoords2[0], turf.getCoord(intersectPoint2)]];
          afterLineCoords = [...[turf.getCoord(intersectPoint1), intersectSegmentCoords1[1]], ...afterLineCoords];
          betweenLineCoords = [];
          betweenLineCoords.push(turf.getCoord(intersectPoint2));
          betweenLineCoords.push(intersectSegmentCoords2[1]);
          for(let i = nearSegmentIndex2 + 1;i < nearSegmentIndex1;i++) {
            let _segment = segments.features[i];
            let _coords = turf.getCoords(_segment);
            betweenLineCoords.push(..._coords);
          }
          betweenLineCoords.push(intersectSegmentCoords1[0]);
          betweenLineCoords.push(turf.getCoord(intersectPoint1));

        }
      }
      // console.log("lineCoords", turf.getCoords(line));
      // console.log("frontLineCoords", frontLineCoords);
      // console.log("betweenLineCoords", betweenLineCoords);
      // console.log("afterLineCoords", afterLineCoords);
      return {
        start: turf.lineString(frontLineCoords),
        finish: turf.lineString(afterLineCoords),
        between: turf.lineString(betweenLineCoords),
        rotate: turf.bearing(betweenLineCoords[0], betweenLineCoords[betweenLineCoords.length - 1]),
        intersects: intersects
      };
    }

    return {
      start: null,
      finish: null,
      between: null,
      rotate: null,
      intersects: intersects
    };

  }
}
export default new SliceUtil();